$(function () {
    function jqueryajax(url,params,successcallback) {
	$.ajax({
	    type: 'GET',
	    url: url,
	    dataType: 'jsonp',
	    data: params,
	    success: function(data) {
		successcallback.apply(this, arguments);
	    },
	    error: function (request, statuss, error) {
		alert("提交失败!");
	    }
	})
    }

    var chartData = [];
    function ProjectData (data) {
	var strInner = '';
	for (var i = 0; i < data.length; i++){
	    var ProjectName = data[i].ProjectName;
	    var ProjectNumber = data[i].ProjectNumber;
	    var PassNumber = data[i].PassNumber;
 	    var PjNa = data[i].PjNa;
	    strInner = strInner + '<tr class="index-bbb" onclick="window.open('+"'http://106.14.1.226:9092/details/?ProjectName="+PjNa+"','_self'"+')"><td>'+ProjectName+'</td><td>'+"无"+'</td><td>'+'无'+'</td><td>'+ProjectNumber+'</td><td>'+PassNumber+'</td><td>'+'无'+'</td></tr>'; 
	}
        $('#index-tbody')[0].innerHTML = strInner;
	chartData = data;
	for (var i = 0; i < chartData.length; i++) {
	    var temp = '';
	    for (var j = i+1; j < chartData.length; j++ ) {
		var number1 = parseInt(chartData[i].ProjectNumber2);
		var number2 = parseInt(chartData[j].ProjectNumber2);
		if (number1 > number2){
		    temp = chartData[i];
		}else {
		    temp = chartData[j];
		    chartData[j] = chartData[i];
		    chartData[i] = temp ;
		}
		
	    }
	}
	console.log(chartData);
    	chart_init(); 
    }
    
    jqueryajax("/indexData/","",ProjectData);
    
//function initMap() {
    var pName = 'china';
    var dom = document.getElementById("section1-map");
    var myChart2 = echarts.init(dom);
    initEcharts(pName);
    function initEcharts(pName){
	var option2 = {
	    title: {
		show: false
	    },
	    roam: true,  //地图的放大缩小
	    legend: {
		orient: 'vertical',
		left: 'left',
		data:['中国移动','中国联通','中国电信'],
	    },
	    series:[
	    {
		name: '中国移动',
		type: 'map',
		mapType: pName,
		//地图样式
		itemStyle:{
		    normal: {
			show: true,
			areaColor: '#0fd2f8',
			borderColor: '#FCFCFC',
			borderWidth: '1'
		    },
		    emphasis: {
			show: true,
			areaColor: '#ff5d24'
		    },
		},
		//图形上的文本标签，可用于说明图形的一些数据信息
		label: {
		    normal: {
			show: true
		    },
		    emphasis: {
			show: true
		    }
		},
		data:[
                    {name: '北京',value: randomData() },
                    {name: '天津',value: randomData() },
                    {name: '上海',value: randomData() },
                    {name: '重庆',value: randomData() },
                    {name: '河北',value: randomData() },
                    {name: '河南',value: randomData() },
                    {name: '云南',value: randomData() },
                    {name: '辽宁',value: randomData() },
                    {name: '黑龙江',value: randomData() },
                    {name: '湖南',value: randomData() },
                    {name: '安徽',value: randomData() },
                    {name: '山东',value: randomData() },
                    {name: '新疆',value: randomData() },
                    {name: '江苏',value: randomData() },
                    {name: '浙江',value: randomData() },
                    {name: '江西',value: randomData() },
                    {name: '湖北',value: randomData() },
                    {name: '广西',value: randomData() },
                    {name: '甘肃',value: randomData() },
                    {name: '山西',value: randomData() },
                    {name: '内蒙古',value: randomData() },
                    {name: '陕西',value: randomData() },
                    {name: '吉林',value: randomData() },
                    {name: '福建',value: randomData() },
                    {name: '贵州',value: randomData() },
                    {name: '广东',value: randomData() },
                    {name: '青海',value: randomData() },
                    {name: '西藏',value: randomData() },
                    {name: '四川',value: randomData() },
                    {name: '宁夏',value: randomData() },
                    {name: '海南',value: randomData() },
                    {name: '台湾',value: randomData() },
                    {name: '香港',value: randomData() },
                    {name: '澳门',value: randomData() }
                ]
	    },
	    {
		name: '中国联通',
		type: 'map',
		mapType: pName,
		itemStyle: {
		    normal: {
			show: true,
			areaColor: '#007bc7',
			borderColor: '#FCFCFC',
			borderWidth: '1'
		    },
		    emphasis: {
			show: true,
			areaColor: '#ff5d24'
		    },
		    emphasis: {
			show: true
		    }
		},
                label: {
		    normal: {
			show: true
		    },
		    emphasis: {
			show: true
		    }
		},
		data:[
                    {name: '北京',value: randomData() },
                    {name: '天津',value: randomData() },
                    {name: '上海',value: randomData() },
                    {name: '重庆',value: randomData() },
                    {name: '河北',value: randomData() },
                    {name: '安徽',value: randomData() },
                    {name: '新疆',value: randomData() },
                    {name: '浙江',value: randomData() },
                    {name: '江西',value: randomData() },
                    {name: '山西',value: randomData() },
                    {name: '内蒙古',value: randomData() },
                    {name: '吉林',value: randomData() },
                    {name: '福建',value: randomData() },
                    {name: '广东',value: randomData() },
                    {name: '西藏',value: randomData() },
                    {name: '四川',value: randomData() },
                    {name: '宁夏',value: randomData() },
                    {name: '香港',value: randomData() },
                    {name: '澳门',value: randomData() }
            	]
	    },
	    {
		name: '中国电信',
		type: 'map',
		mapType: pName,
		itemStyle: {
		    normal: {
			show: true,
			areaColor: '#007bc7',
			borderColor: '#FCFCFC',
			borderWidth: '1'
		    },
		    emphasis: {
			show: true,
			areaColor: '#ff5d24'
		    }
		},
		label: {
		    normal: {
			show: true
		    },
		    emphasis: {
			show: true
		    }
		},
		data:[
                    {name: '北京',value: randomData() },
                    {name: '天津',value: randomData() },
                    {name: '上海',value: randomData() },
                    {name: '广东',value: randomData() },
                    {name: '台湾',value: randomData() },
                    {name: '香港',value: randomData() },
                    {name: '澳门',value: randomData() }
                ]
	    }
	    ]
	};
	myChart2.setOption(option2);
    };
    var provincesText = ['上海', '河北', '山西', '内蒙古', '辽宁', '吉林','黑龙江',  '江苏', '浙江', '安徽', '福建', '江西', '山东','河南', '湖北', '湖南', '广东', '广西', '海南', '四川', '贵州', '云南', '西藏', '陕西', '甘肃', '青海', '宁夏', '新疆', '北京', '天津', '重庆', '香港', '澳门'];
    myChart2.on('click',function (parm) {
	var sign = 0;
	for(var i=0; i<provincesText.length; i++){
	    if(parm.name == provincesText[i]){
		initEcharts(parm.name);
		sign = 1;
		continue;
	    }
	}
	if(sign == 0){
	    initEcharts('china');
	}
    });
    function randomData(){
	return Math.round(Math.random()*1000);
    }

//}

 
  



    function chart_init () {
  	//初始化echarts实例
        var myChart = echarts.init($("#main")[0]);
        //添加点击事件
        //var ecConfig = require('echarts/config');
	//myChart.on(ecConfig.EVENT.CLICK, eConsole);
  	//指定图表的配置项和数据
  var option = {
    title: {
      text: ''
    },
    tooltip: {formatter:'{b}\n{c}%'},
    legend: {},
    xAxis: {
      data: [chartData[0].ProjectName,chartData[1].ProjectName,chartData[2].ProjectName,chartData[3].ProjectName,chartData[4].ProjectName,chartData[5].ProjectName,chartData[6].ProjectName,chartData[7].ProjectName,chartData[8].ProjectName,chartData[9].ProjectName],
      axisLine: {
        lineStyle: {
          color: '#ffffff',
        }
      }
    },
    yAxis: [{
        type: 'value',
        axisLabel: {
          show: true,
          interval: 'auto',
          formatter: '{value}%'
        },
        // show: true,

      axisLine: {
        lineStyle: {
          color: '#ffffff',
        }
      }
    }],
    series: [{
      name: '进度',
      type: 'bar',
      data: [
        {value:parseFloat(chartData[0].PassNumber),itemStyle:{normal:{color: 'red',label:{show:true,position:'top',formatter:'{b}\n{c}%'}}}},
        {value:parseFloat(chartData[1].PassNumber),itemStyle:{normal:{color: 'yellow',label:{show:true,position:'top',formatter:'{b}\n{c}%'}}}},
        {value:parseFloat(chartData[2].PassNumber),itemStyle:{normal:{color: 'pink',label:{show:true,position:'top',formatter:'{b}\n{c}%'}}}},
        {value:parseFloat(chartData[3].PassNumber),itemStyle:{normal:{color: 'yellow',label:{show:true,position:'top',formatter:'{b}\n{c}%'}}}},
        {value:parseFloat(chartData[4].PassNumber),itemStyle:{normal:{color: 'red',label:{show:true,position:'top',formatter:'{b}\n{c}%'}}}},
        {value:parseFloat(chartData[5].PassNumber),itemStyle:{normal:{color: 'orange',label:{show:true,position:'top',formatter:'{b}\n{c}%'}}}},
        {value:parseFloat(chartData[6].PassNumber),itemStyle:{normal:{color: 'orange',label:{show:true,position:'top',formatter:'{b}\n{c}%'}}}},
        {value:parseFloat(chartData[7].PassNumber),itemStyle:{normal:{color: 'orange',label:{show:true,position:'top',formatter:'{b}\n{c}%'}}}},
        {value:parseFloat(chartData[8].PassNumber),itemStyle:{normal:{color: 'orange',label:{show:true,position:'top',formatter:'{b}\n{c}%'}}}},
        {value:parseFloat(chartData[9].PassNumber),itemStyle:{normal:{color: 'orange',label:{show:true,position:'top',formatter:'{b}\n{c}%'}}}},
    ]
  }],
}


  //使用刚指定的配置项和数据显示图表
  myChart.setOption(option);
  var ecConfig = require('echarts/config');
  function eConsole(param) {
      if (typeof param.seriesIndex != 'undefined') {
	  switch (param.name) {
	      case chartData[0].ProjectName:
		  window.location.href = "/details/?ProjectName="+chartData[0].ProjectName;
		  break;
	      case chartData[1].ProjectName:
		  window.location.href = "/details/?ProjectName="+chartData[1].ProjectName;
		  break;
	      case chartData[2].ProjectName:
		  window.location.href = "/details/?ProjectName="+chartData[2].ProjectName;
		  break;
	      case chartData[3].ProjectName:
		  window.location.href = "/details/?ProjectName="+chartData[3].ProjectName;
		  break;
	      case chartData[4].ProjectName:
		  window.location.href = "/details/?ProjectName="+chartData[4].ProjectName;
		  break;
	      case chartData[5].ProjectName:
		  window.location.href = "/details/?ProjectName="+chartData[5].ProjectName;
		  break;
	      case chartData[6].ProjectName:
		  window.location.href = "/details/?ProjectName="+chartData[6].ProjectName;
		  break;
	      case chartData[7].ProjectName:
		  window.location.href = "/details/?ProjectName="+chartData[7].ProjectName;
		  break;
	      case chartData[8].ProjectName:
		  window.location.href = "/details/?ProjectName="+chartData[8].ProjectName;
		  break;
	      case chartData[9].ProjectName:
		  window.location.href = "/details/?ProjectName="+chartData[9].ProjectName;
		  break;
	  }	
      }
  }
  myChart.on(ecConfig.EVENT.CLICK, eConsole);
}
});

